﻿
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.IApplication;
using Dora.Services.AdminUser.Repository.Repositorys;
using Dora.Services.AdminUser.Application.Base;
using Dora.Tools.Utility.Const;
using Microsoft.Extensions.Logging;
using Surging.Core.Common;
using Surging.Core.CPlatform.Ioc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dora.Services.AdminUser.Repository;
using Dora.Services.AdminUser.Dto.SaveDto;
using Mapster;

namespace Dora.Services.AdminUser.Application
{

    /// <summary>
    /// 大社产品类型/目的地保险产品默认排除供应商不绑定保险服务
    /// </summary>
    [ModuleName(DoraModuleName.AdminUser)]
    public class OrgInsuranceExcludeService : DoraServiceBase, IOrgInsuranceExcludeService
    {
        private ISugarUnitOfWork _unitOfWork;
        private readonly OrgInsuranceSupplierRepository _repository;

        private readonly OrgInsuranceSupplierAreaRepository _areaRepository;
        public OrgInsuranceExcludeService(ISugarUnitOfWork unitOfWork) 
        {
            _unitOfWork = unitOfWork;
            _repository = new OrgInsuranceSupplierRepository(unitOfWork);
            _areaRepository = new OrgInsuranceSupplierAreaRepository(unitOfWork);
        }

        public async Task<ResultDto<List<OrgInsuranceSupplierAreaEntity>>> BatchInsertOrgInsuranceSupplierAreas(List<OrgInsuranceSupplierAreaEntity> OrgInsuranceExcludes)
        {
            return await _areaRepository.BatchInsertOrgInsuranceSupplierAreas(OrgInsuranceExcludes);
        }
        public async Task<bool> BatchDelOrgInsuranceSupplierAreas(List<OrgInsuranceSupplierAreaEntity> OrgInsuranceExcludes)
        {
            return await _areaRepository.BatchDelOrgInsuranceSupplierAreas(OrgInsuranceExcludes);
        }

        public async Task<ResultDto<List<OrgInsuranceSupplierEntity>>> BatchInsertOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes) 
        {
            return await _repository.BatchInsertOrgInsuranceExcludes(OrgInsuranceExcludes);
        }

        public async Task<ResultDto<Boolean>> BatchUpdateOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes)
        {
            return await _repository.BatchUpdateOrgInsuranceExcludes(OrgInsuranceExcludes);
        }

        public async Task<bool> BatchDelOrgInsuranceExcludes(List<OrgInsuranceSupplierEntity> OrgInsuranceExcludes)
        {
            return await _repository.BatchDelOrgInsuranceExcludes(OrgInsuranceExcludes);
        }
        public async Task<List<OrgInsuranceSupplierSaveDto>> GetAllByOrgId(int orgId, int BindType, int BindId)
        {

            List<OrgInsuranceSupplierSaveDto> views = new List<OrgInsuranceSupplierSaveDto>(); 
            List <OrgInsuranceSupplierEntity> supplierEntities = await _repository.GetAllByOrgId(orgId, BindType, BindId);

            
            for (int i = 0; supplierEntities!=null && i < supplierEntities.Count; i++)
            {
                OrgInsuranceSupplierSaveDto supplierSaveDto = supplierEntities[i].Adapt<OrgInsuranceSupplierSaveDto>();
                List<OrgInsuranceSupplierAreaEntity> areaEntitys = await _areaRepository.GetAllByOrgId(orgId, supplierSaveDto.SettingId, supplierSaveDto.Id);
                supplierSaveDto.OrgInsuranceSupplierAreas = areaEntitys.Adapt<List<OrgInsuranceSupplierAreaSaveDto>>();
                views.Add(supplierSaveDto);
            }
            return views;
        }
    }
}